import 'package:flutter/material.dart';

import 'SearchBar.dart';

enum PlusMenu { newChatMenu, addContanctsMenu, ScanMenu, MoneyMenu }

class WeChatsPage extends StatelessWidget {
	final String title;

	WeChatsPage({Key key, @required this.title}) : super(key: key);

	@override
	Widget build(BuildContext context) {
		return Scaffold(
			backgroundColor: Colors.grey,
			appBar: AppBar(
				title: Text(title),
				actions: <Widget>[
					PopupMenuButton<PlusMenu>(
						icon: Icon(Icons.add),
						offset: Offset(0, 16.0),
						onSelected: (PlusMenu result) {
							print(result);
						},
						itemBuilder: (BuildContext context) => <PopupMenuEntry<PlusMenu>> [
							PopupMenuItem<PlusMenu>(
								value: PlusMenu.newChatMenu,
								child: Row(
									children: <Widget>[
										Icon(Icons.chat),
										Text('New Chat')
									],
								),
							),
							PopupMenuItem<PlusMenu>(
								value: PlusMenu.addContanctsMenu,
								child: Row(
									children: <Widget>[
										Icon(Icons.contacts),
										Text('Add Contacts')
									],
								),
							),
							PopupMenuItem<PlusMenu>(
								value: PlusMenu.ScanMenu,
								child: Row(
									children: <Widget>[
										Icon(Icons.scanner),
										Text('Scan')
									],
								),
							),
							PopupMenuItem<PlusMenu>(
								value: PlusMenu.MoneyMenu,
								child: Row(
									children: <Widget>[
										Icon(Icons.attach_money),
										Text('Money')
									],
								),
							),
						],
					),
				],
			),
			body: CustomScrollView(
				slivers: <Widget>[
					// SearchBar(),
					SliverList(
						delegate: SliverChildListDelegate([
							Column(
								children: <Widget>[
									SearchBar(),
									ListView.builder(
										itemCount: 200,
										itemBuilder: (context, index) {
											return WeChatBox(index: index,);
										},
										shrinkWrap: true,  // 必须设置为true
										physics: ClampingScrollPhysics(),  // 必须设置为ClampingScrollPhysics
									)
								],
							)
						]),
					)
				],
			),
		);
	}
}

class WeChatBox extends StatelessWidget {

	final int index;

	WeChatBox({Key key, this.index}) : super(key: key);

	@override
	Widget build(BuildContext context) {
		return Container(
			color: Colors.white,
			child: Column(
				children: <Widget>[
					ListTile(
						leading: Image.asset(
							'assets/icons/avatar${index%4}.png',
							width: 50.0,
						),
						title: Text('Friend'),
						subtitle: Text(
							'friend............',
							style: TextStyle(
								color: Colors.grey
							),
						),
						trailing: Column(
							children: <Widget>[
								Text(
									'yesterday',
									style: TextStyle(
										color: Colors.grey
									),
								),
								Icon(
									Icons.child_friendly,
									color: Colors.grey,
								),
							],
						)
					),
					Divider(
						height: 1.0,
						color: Colors.black,
					)
				],
			),
		);
	}
}
